package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.EntrustOrder;
import com.hyk.dto.R;
import com.hyk.model.EntrustOrderParam;
import com.hyk.service.EntrustOrderService;
import com.hyk.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@RestController
@Api(tags = "交易委托控制器")
@RequestMapping("/entrustOrders")
public class EntrustOrdersController {

    @Resource
    private EntrustOrderService entrustOrderService;

    @GetMapping
    @ApiOperation("按条件分页查询")
    public R<Page<EntrustOrder>> listForPage(@ApiIgnore Page<EntrustOrder> page,
                                             Byte type,
                                             Long userId,
                                             Long id,
                                             String userName,
                                             String startTime,
                                             String endTime,
                                             Byte status,
                                             String mobile,
                                             Long marketId,
                                             String symbol){
        Page<EntrustOrder> page1 = entrustOrderService.listForPage(page,type,userId,id,userName,startTime,endTime,status,mobile,marketId,symbol);
        return R.ok(page1);
    }

    @GetMapping("/{symbol}")
    public R<Page<EntrustOrder>> list(@ApiIgnore Page<EntrustOrder> page,
                                      @PathVariable String symbol){
        Page<EntrustOrder> page1 = entrustOrderService.page(page, new LambdaQueryWrapper<EntrustOrder>().
                eq(EntrustOrder::getUserId, UserUtils.getUser()).
                eq(EntrustOrder::getSymbol, symbol).
                eq(EntrustOrder::getStatus,0).
                orderByDesc(EntrustOrder::getCreated));
        return R.ok(page1);
    }

    @PostMapping
    @ApiOperation("新增买入委托单")
    public R buy(@RequestBody EntrustOrderParam entrustorderParam){
        entrustOrderService.buy(entrustorderParam);
        return R.ok();
    }

    @GetMapping("/history/{symbol}")
    public R<Page<EntrustOrder>> historyList(Page<EntrustOrder> page,
                                             @PathVariable String symbol){

        Page<EntrustOrder> list = entrustOrderService.page(page, new LambdaQueryWrapper<EntrustOrder>()
                .eq(EntrustOrder::getUserId, UserUtils.getUser())
                .eq(EntrustOrder::getSymbol, symbol)
                .in(EntrustOrder::getStatus,1,2)
                .orderByDesc(EntrustOrder::getCreated)
        );
        return R.ok(list);
    }

}
